1
|
|
|
/* |
2
|
|
|
* byte-padding: Funcions to pad bits and bytes. |
3
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
4
|
|
|
* https://github.com/rochars/byte-data |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Padding for binary strings. |
9
|
|
|
* @param {!Array<string>} bytes The bytes as binary strings. |
10
|
|
|
* @param {number} base The base. |
11
|
|
|
* @param {number} index The byte to pad. |
12
|
|
|
*/ |
13
|
|
|
function padding(bytes, base, index) { |
14
|
|
|
bytes[index] = bytePadding(bytes[index], base); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Padding with 0s for byte strings. |
19
|
|
|
* @param {string} byte The byte as a binary or hex string. |
20
|
|
|
* @param {number} base The base. |
21
|
|
|
* @returns {string} The padded byte. |
22
|
|
|
*/ |
23
|
|
|
function bytePadding(byte, base) { |
24
|
|
|
let offset = byte.length + 1; |
25
|
|
|
if (base == 2) { |
26
|
|
|
offset = 8; |
27
|
|
|
} else if (base == 16) { |
28
|
|
|
offset = 2; |
29
|
|
|
} |
30
|
|
|
if (byte.length < offset) { |
31
|
|
|
byte = new Array((offset + 1 - byte.length)).join("0") + byte; |
32
|
|
|
} |
33
|
|
|
return byte; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Fix the size of nibbles. |
38
|
|
|
* @param {!Array<string>} nibbles The nibble as a binary or hex string. |
39
|
|
|
* @param {number} base The base. |
40
|
|
|
* @param {number} index The nibble offset. |
41
|
|
|
*/ |
42
|
|
|
function paddingNibble(nibbles, base, index) { |
43
|
|
|
if (base == 2 && nibbles[index].length < 4) { |
44
|
|
|
nibbles[index] = |
45
|
|
|
new Array((5 - nibbles[index].length)).join("0") + nibbles[index]; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Fix the size of crumbs. |
51
|
|
|
* @param {!Array<string>} crumbs The nibble as a binary or hex string. |
52
|
|
|
* @param {number} base The base. |
53
|
|
|
* @param {number} index The nibble offset. |
54
|
|
|
*/ |
55
|
|
|
function paddingCrumb(crumbs, base, index) { |
56
|
|
|
if ((base == 2 || base == 16) && crumbs[index].length < 2) { |
57
|
|
|
crumbs[index] = '0' + crumbs[index]; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Pad a string with zeros to the left. |
63
|
|
|
* TODO: This should support both arrays and strings. |
64
|
|
|
* @param {string} value The string (representing a binary or hex value). |
65
|
|
|
* @param {number} numZeros the max number of zeros. |
66
|
|
|
* For 1 binary byte string it should be 8. |
67
|
|
|
*/ |
68
|
|
|
function lPadZeros(value, numZeros) { |
69
|
|
|
let i = 0; |
|
|
|
|
70
|
|
|
while (value.length < numZeros) { |
71
|
|
|
value = '0' + value; |
72
|
|
|
} |
73
|
|
|
return value; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Pad a array with zeros to the right. |
78
|
|
|
* @param {!Array<number>} byteArray The array. |
79
|
|
|
* @param {number} numZeros the max number of zeros. |
80
|
|
|
* For 1 binary byte string it should be 8. |
81
|
|
|
* TODO: better explanation of numZeros |
82
|
|
|
*/ |
83
|
|
|
function fixByteArraySize(byteArray, numZeros) { |
84
|
|
|
let i = 0; |
85
|
|
|
let fix = byteArray.length % numZeros; |
86
|
|
|
if (fix) { |
87
|
|
|
fix = (fix - numZeros) * -1; |
88
|
|
|
while(i < fix) { |
89
|
|
|
byteArray.push(0); |
90
|
|
|
i++; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
module.exports.fixByteArraySize = fixByteArraySize; |
96
|
|
|
module.exports.padding = padding; |
97
|
|
|
module.exports.paddingNibble = paddingNibble; |
98
|
|
|
module.exports.paddingCrumb = paddingCrumb; |
99
|
|
|
module.exports.bytePadding = bytePadding; |
100
|
|
|
module.exports.lPadZeros = lPadZeros; |
101
|
|
|
|